package com.baosight.sczx.zm.ck.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ck.domain.ZMCKKCSB;

/**
 * 排班
 * <p>Title: ServiceZMCG71.java</p> 
 */
public class ServiceZMCK33 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
//		inInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));		 
		inInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) { 
		if(null != inInfo.getBlock("inqu_status")){
			String fdKcsbName = inInfo.getBlock("inqu_status").getCellStr(0, "fdKcsbName");
			String fdKcsbXh = inInfo.getBlock("inqu_status").getCellStr(0, "fdKcsbXh");
			if(null != fdKcsbName && !("").equals(fdKcsbName)){
				StaticTools.setQueryParameters(inInfo, "fdKcsbName", fdKcsbName, true);//添加查询条件 
			}
			if(null != fdKcsbXh && !("").equals(fdKcsbXh)){
				StaticTools.setQueryParameters(inInfo, "fdKcsbXh", fdKcsbXh, true);//添加查询条件 
			}
			}
		String nf = inInfo.getString("nf");
	    if(null != nf && !("").equals(nf)){
	    	String fdBeginDate = nf+"-01-01";
	    	String fdEndDate = nf+"-12-31";
	    	StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);//添加查询条件 
	    	StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);//添加查询条件 
	    	StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);//添加查询条件 
	    }
//		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
//		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件	
		EiInfo outInfo = super.query(inInfo, "ZMCKKCSB.query", new ZMCKKCSB());
//		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));		 
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ck.kcsbName", false));//库存水泵名称
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ck.kcsbXh", false));//库存水泵型号
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ck.kcsbDjXh", false));//库存水泵电机型号
		outInfo.addBlock(getMines());
		outInfo.set("fdDepartmentId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdDepartment", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		
		return outInfo;
	}

	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGTsPg.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

/**
 * <p>Function: insert</p>
 * <p>Description: 新增</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午03:07:57 张坤
 * </p>
 *
 */
@Override	
public EiInfo insert(EiInfo inInfo) {
	EiInfo out=new EiInfo(); 		
	String date = StaticTools.getDate();
	String fdStatus = "草拟";
//	String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
	ZMCKKCSB _dom = new ZMCKKCSB();
//	String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
//	String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
	String fdReportTime = StaticTools.getDateTime();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		_dom.setFdCurrentDate(date);
//		_dom.setFdReportPerson(fdReportPerson);
//		_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
//		_dom.setFdMiningId(fdMiningId);
//		_dom.setFdMiningName(fdMiningName);
//		_dom.setFdReportTime(fdReportTime);
		_dom.setFdStatus(fdStatus);
		_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_KCSB"));
		this.dao.insert("ZMCKKCSB.insert",_dom); 
	}
	out = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return out;  
}

/**
 * <p>Function: update</p>
 * <p>Description: 修改</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午04:20:50 张坤
 * </p>
 *
 */
@Override
public EiInfo update(EiInfo inInfo) {
	//校验是否重复
	EiInfo out=new EiInfo();
	String date = StaticTools.getDate();
//	String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
//	String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
//	String fdReportPerson = (String) UserSession.getInSessionProperty("UserCname");
//	String fdReportTime = StaticTools.getDateTime();
	ZMCKKCSB _dom = new ZMCKKCSB();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		if(validate(_dom)){
		_dom.setFdCurrentDate(date);
//		_dom.setFdReportPerson(fdReportPerson);
//		_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
//		_dom.setFdMiningId(fdMiningId);
//		_dom.setFdMiningName(fdMiningName);
//		_dom.setFdReportTime(fdReportTime);
		this.dao.update("ZMCKKCSB.update",_dom);
		}else{
			out = this.query(inInfo);
			out.setStatus(-1);
			out.setMsg("只能修改草拟状态的记录!"); 
			return out;
		}
		
	}
	out = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return out;  
	
}	

/**
 * <p>Function: delete</p>
 * <p>Description: 删除</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午04:30:17 张坤
 * </p>
 *
 */
@Override
public EiInfo delete(EiInfo inInfo) { 
	EiInfo out=new EiInfo(); 
	ZMCKKCSB _dom = new ZMCKKCSB();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		if(validate(_dom)){
		this.dao.delete("ZMCKKCSB.delete", _dom);
		}else{
			out = this.query(inInfo);
			out.setStatus(-1);
			out.setMsg("只能删除草拟状态的记录!"); 
			return out;
		}
	}
	EiInfo outInfo = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return outInfo;

}	

/**
 * <p>Function: delete</p>
 * <p>Description: 删除</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午04:30:17 张坤
 * </p>
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public EiInfo report(EiInfo inInfo) {
	EiInfo out=new EiInfo(); 
	ZMCKKCSB _dom = new ZMCKKCSB();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		if(validate(_dom)){
		this.dao.update("ZMCKKCSB.report",_dom);
		}else{
			out = this.query(inInfo);
			out.setStatus(-1);
			out.setMsg("只能上报草拟状态的记录!"); 
			return out;
		}
	}
	EiInfo outInfo = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return outInfo;
}	

/**
 * <p>Function: validate</p>
 * <p>Description: 对主表记录新增，删除，上报前的后台校验</p>
 * <p>return value:boolean</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午03:40:23张坤
 * </p>
 */
private boolean validate(ZMCKKCSB _dom){
	  String fdStatus = _dom.getFdStatus();
	  if("草拟".equals(fdStatus)){
		 return true; 
	  }else
		  return false;
	  
}



}

